﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace lcs.Web.LED
{
    /// <summary>
    /// GetLED 的摘要说明
    /// </summary>
    public class GetLED : IHttpHandler, IRequiresSessionState
    {
        Maticsoft.BLL.supplyPlan bll = new Maticsoft.BLL.supplyPlan();
        Maticsoft.BLL.monthSupplayPlan mbll = new Maticsoft.BLL.monthSupplayPlan();
        Maticsoft.BLL.warnInfo wbll = new Maticsoft.BLL.warnInfo();
        lcs.BLL.distribution dbll = new BLL.distribution();

        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
                Query(context);
            else if (ac == "pro")
                QueryPro(context);
            else if (ac == "ddllist")
                ddllist(context);
            else if (ac == "arrival")
                QueryArrival(context);
            else if (ac == "timeout")
                QueryTimeout(context);
            else if (ac == "warn")
                QueryWarn(context);

        }
        public void QueryWarn(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //if (Web.Admin.myinfo.isLogin(context))
            {
                //int uid = Web.Admin.myinfo.getID(context);

                //获取前台传来的值 
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());

                }
                if (null != context.Request.Form["page"])
                {

                    page = int.Parse(context.Request.Form["page"].ToString().Trim());

                }
                if (null != context.Request.Form["sort"])
                {

                    order = context.Request.Form["sort"].ToString().Trim();

                }
                if (null != context.Request.Form["order"])
                {

                    sort = context.Request.Form["order"].ToString().Trim();

                }
                StringBuilder strWhere = new StringBuilder();
                //strWhere.Append(" supplyDate<GETDATE() and lastArrivalDate is null");

                if (sort != "" && order != "")
                {
                    //strWhere.AppendFormat(" order by {0} {1}", sort, order);//添加排序  
                    oderby = order + " " + sort;
                }
                //DataSet ds = Bnotice.GetList(strWhere.ToString());  //调用不分页的getlist  

                //调用分页的GetList方法  
                DataSet ds = wbll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = wbll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }
        public void QueryTimeout(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //if (Web.Admin.myinfo.isLogin(context))
            {
                //int uid = Web.Admin.myinfo.getID(context);
                
                //获取前台传来的值 
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());

                }
                if (null != context.Request.Form["page"])
                {

                    page = int.Parse(context.Request.Form["page"].ToString().Trim());

                }
                if (null != context.Request.Form["sort"])
                {

                    order = context.Request.Form["sort"].ToString().Trim();

                }
                if (null != context.Request.Form["order"])
                {

                    sort = context.Request.Form["order"].ToString().Trim();

                }
                StringBuilder strWhere = new StringBuilder();                
                strWhere.Append(" supplyDate<GETDATE() and lastArrivalDate is null");

                if (sort != "" && order != "")
                {
                    //strWhere.AppendFormat(" order by {0} {1}", sort, order);//添加排序  
                    oderby = order + " " + sort;
                }
                //DataSet ds = Bnotice.GetList(strWhere.ToString());  //调用不分页的getlist  

                //调用分页的GetList方法  
                DataSet ds = mbll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = 7;// mbll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }
        public void ddllist(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //int uid = Web.Admin.myinfo.getID(context);
            //if (uid != 0)
            Maticsoft.BLL.projectType mbll = new Maticsoft.BLL.projectType();
            {                
                DataSet ds = mbll.GetList("");
                string strJson = ToJson.DataTable2Json1(ds.Tables[0]);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.ApplicationInstance.CompleteRequest();
            }            
        }
        public void QueryPro(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int sum = 0, sump = 0; 
            int oversum = 0,caid=0,oversump=0;
            string StartTime = "", EndTime = "", strcaid = "", projectName = "" ;
            if (null != context.Request.QueryString["StartTime"])
            {//获取前台传来的值  
                StartTime = context.Request.QueryString["StartTime"].ToString().Trim();
            }
            if (null != context.Request.QueryString["EndTime"])
            {//获取前台传来的值  
                EndTime = context.Request.QueryString["EndTime"].ToString().Trim();
            }
            if (null != context.Request.QueryString["caid"])
            {//获取前台传来的值  
                strcaid = context.Request.QueryString["caid"].ToString().Trim();
                int.TryParse(strcaid, out caid);
            }
            if (null != context.Request.QueryString["projectName"])
            {//获取前台传来的值  
                projectName = context.Request.QueryString["projectName"].ToString().Trim();
            }
            StringBuilder strWhere = new StringBuilder();
            if (StartTime != "")
            {
                strWhere.AppendFormat(" supplyDate>='{0}' and ", StartTime);
            }
            if (EndTime != "")
            {
                strWhere.AppendFormat(" supplyDate <= '{0}' and ", EndTime);
            }
            if (caid != 0)
            {
                strWhere.AppendFormat(" projectTypeId = '{0}' and ", caid);
            }
            
            if (projectName !="")
            {
                strWhere.AppendFormat(" projectName = '{0}' and ", projectName);
            }


            int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
            if (startindex >= 0)
            {
                strWhere.Remove(startindex, 3);//删除多余的and关键字  
            }
            sum = bll.GetRecordSum(strWhere.ToString());
            oversum = bll.GetRecordOverSum(strWhere.ToString().Replace("supplyDate", "supplyPlan.supplyDate"));
            sump = bll.GetRecordSumPrice(strWhere.ToString());
            oversump = bll.GetRecordOverSumPrice(strWhere.ToString().Replace("supplyDate", "supplyPlan.supplyDate"));
            string json = "[{\"sum\":\"" + sum + "\",\"oversum\":\"" +oversum + "\",\"sump\":\"" + sump + "\",\"oversump\":\"" + oversump + "\"}]";
            context.Response.Write(json);
            context.ApplicationInstance.CompleteRequest();
        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //if (Web.Admin.myinfo.isLogin(context))
            {
                //int uid = Web.Admin.myinfo.getID(context);
                string uname, utruename, st;
                uname = utruename = st = "";
                //获取前台传来的值 
                if (null != context.Request.Form["search_type"])
                {//获取前台传来的值  
                    st = context.Request.Form["search_type"].ToString().Trim();
                }
                if (null != context.Request.Form["search_value"])
                {//获取前台传来的值  
                    uname = context.Request.Form["search_value"].ToString().Trim();
                }
                if (st == "username")
                {
                    utruename = uname;
                    uname = "";
                }
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());

                }
                if (null != context.Request.Form["page"])
                {

                    page = int.Parse(context.Request.Form["page"].ToString().Trim());

                }
                if (null != context.Request.Form["sort"])
                {

                    order = context.Request.Form["sort"].ToString().Trim();

                }
                if (null != context.Request.Form["order"])
                {

                    sort = context.Request.Form["order"].ToString().Trim();

                }
                StringBuilder strWhere = new StringBuilder();
                if (uname != "")
                {
                    strWhere.AppendFormat(" uname like '%{0}%' and ", uname);
                }
                if (utruename != "")
                {
                    strWhere.AppendFormat(" utruename like '%{0}%' and ", utruename);
                }

                //strWhere.AppendFormat(" cuid={0} and ", uid);
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    //strWhere.AppendFormat(" order by {0} {1}", sort, order);//添加排序  
                    oderby = order + " " + sort;
                }
                //DataSet ds = Bnotice.GetList(strWhere.ToString());  //调用不分页的getlist  

                //调用分页的GetList方法  
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                //string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write("");//返回给前台页面  
                context.Response.End();
            }

        }

        public void QueryArrival(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //if (Web.Admin.myinfo.isLogin(context))
            {
                //int uid = Web.Admin.myinfo.getID(context);
                int mins=-10;
                if (null != context.Request.Form["mins"])
                {
                    mins = int.Parse(context.Request.Form["mins"].ToString().Trim());
                    mins = -mins;
                }
                //获取前台传来的值 
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());

                }
                if (null != context.Request.Form["page"])
                {

                    page = int.Parse(context.Request.Form["page"].ToString().Trim());

                }
                if (null != context.Request.Form["sort"])
                {

                    order = context.Request.Form["sort"].ToString().Trim();

                }
                if (null != context.Request.Form["order"])
                {

                    sort = context.Request.Form["order"].ToString().Trim();

                }
                StringBuilder strWhere = new StringBuilder();
                DateTime dt = DateTime.Now.AddMinutes(mins);
                strWhere.AppendFormat(" lastArrivalDate>='{0}' ", dt.ToString());
                
                if (sort != "" && order != "")
                {
                    //strWhere.AppendFormat(" order by {0} {1}", sort, order);//添加排序  
                    oderby = order + " " + sort;
                }
                //DataSet ds = Bnotice.GetList(strWhere.ToString());  //调用不分页的getlist  

                //调用分页的GetList方法  
                DataSet ds = dbll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = dbll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}